package zhuyun.code.datamanager.view

import android.content.Context
import android.graphics.Canvas
import android.graphics.Paint
import android.graphics.Rect
import android.view.View
import androidx.recyclerview.widget.RecyclerView
import zhuyun.code.datamanager.R

/**
 * @author: Administrator
 * @date: 2021/11/2
 */
class SimpleDividerDecoration(private val context: Context): RecyclerView.ItemDecoration() {
    private var dividerHeight :Int = 0
    private var dividerPaint: Paint = Paint()

    init{
        dividerPaint.color = context.resources.getColor(R.color.black)
        dividerHeight = context.resources.getDimensionPixelSize(R.dimen.divider_height)

    }

    override fun getItemOffsets(
        outRect: Rect,
        view: View,
        parent: RecyclerView,
        state: RecyclerView.State
    ) {
        super.getItemOffsets(outRect, view, parent, state)
        outRect.bottom = dividerHeight
    }

    override fun onDraw(c: Canvas, parent: RecyclerView, state: RecyclerView.State) {
        //super.onDraw(c, parent, state)
        val childCount = parent.childCount
        val left = parent.paddingLeft
        val right = parent.width - parent.paddingRight
        for (index in 0 until childCount) {
            val view = parent.getChildAt(index)
            val top = view.bottom
            val bottom = view.bottom + dividerHeight
            c.drawRect(left.toFloat(), top.toFloat(), right.toFloat(), bottom.toFloat(),dividerPaint)

        }
    }

}